package fightplan.diag;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;

import fightplan.core.FPExceptionHandler;
import fightplan.views.ClientView;

@SuppressWarnings("serial")
public class LoginDialog extends JFrame {
	ClientView clientView;
	private JTextField tTatami = new JTextField();
	private JTextField tPerson = new JTextField();

	public LoginDialog() {
		super();
		setTitle("Client Login");

		tTatami.setText("Tatami 1");
		tPerson.setText("");

		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(build(), BorderLayout.CENTER);
		getContentPane().add(buildActions(), BorderLayout.SOUTH);
	}

	private Component build() {
		FormLayout layout = new FormLayout("left:max(40dlu;p), 4dlu, max(120dlu;p):grow(1)");
		DefaultFormBuilder builder = new DefaultFormBuilder(layout);

		builder.setDefaultDialogBorder();

		builder.appendSeparator("Anmeldung");
		builder.append("Tatami", tTatami);
		builder.append("Verantwortlich", tPerson);

		return builder.getContainer();
	}

	public Component buildActions() {
		JPanel panel = new JPanel(new FlowLayout(SwingConstants.RIGHT));
		panel.add(new JButton(new SaveAction()));
		return panel;
	}

	private class SaveAction extends AbstractAction {

		public SaveAction() {
			putValue(Action.NAME, "Start");
		}

		public void actionPerformed(ActionEvent e) {
			LoginDialog.this.setVisible(false);
			ClientView frame = new ClientView(tTatami.getText(), tPerson.getText());
			frame.setSize(900, 700);
			frame.setLocationRelativeTo(null);
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			FPExceptionHandler.instance().setParent(frame);
			frame.setVisible(true);
		}

	}

}
